# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Yuui Tanabe <yuuitanabe@163.com>
# Contributor: Oscar Fuentes <ofv@wanadoo.es>
# Contributor: Adrian Pop <adrian.pop@liu.se>

_compiler=gcc
# "Ninja" cant install each component seperately
# https://github.com/martine/ninja/issues/932
_generator="MSYS Makefiles"
#_generator="Ninja"
if [ "${_generator}" = "Ninja" ]; then
  _make_cmd="ninja"
else
  _make_cmd="make"
fi

_realname=clang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-clang-analyzer"
         "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
         "${MINGW_PACKAGE_PREFIX}-compiler-rt"
         "${MINGW_PACKAGE_PREFIX}-libc++"
         "${MINGW_PACKAGE_PREFIX}-libc++abi"
         "${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-lld"
         "${MINGW_PACKAGE_PREFIX}-lldb"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-openmp"
         "${MINGW_PACKAGE_PREFIX}-polly")
pkgver=11.0.0
pkgrel=5
pkgdesc="C language family frontend for LLVM (mingw-w64)"
arch=('any')
url="https://llvm.org/"
license=("custom:Apache 2.0 with LLVM Exception")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-z3"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-uasm"
             "tar"
             "groff"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-clang" \
               "${MINGW_PACKAGE_PREFIX}-compiler-rt" \
               "${MINGW_PACKAGE_PREFIX}-libc++")
             $([[ "$_generator" == "Ninja" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-ninja")
             )
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('!debug' 'strip')
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver}
source=(${_url}/llvm-${pkgver}.src.tar.xz{,.sig}
        ${_url}/openmp-${pkgver}.src.tar.xz{,.sig}
        ${_url}/clang-${pkgver}.src.tar.xz{,.sig}
        ${_url}/compiler-rt-${pkgver}.src.tar.xz{,.sig}
        ${_url}/test-suite-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libcxx-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libcxxabi-${pkgver}.src.tar.xz{,.sig}
        ${_url}/clang-tools-extra-${pkgver}.src.tar.xz{,.sig}
        ${_url}/lld-${pkgver}.src.tar.xz{,.sig}
        ${_url}/lldb-${pkgver}.src.tar.xz{,.sig}
        ${_url}/libunwind-${pkgver}.src.tar.xz{,.sig}
        ${_url}/polly-${pkgver}.src.tar.xz{,.sig}
        "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch"
        "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch"
        "0005-add-pthread-as-system-lib-for-mingw.patch"
        "0006-add-coff-exported-flag.patch"
        "0007-getLoadAddressWithOffset-assertion.patch"
        "0008-enable-emutls-for-mingw.patch"
        "0009-empty-target-prefix-only-msvc.patch"
        "0010-mbig-obj-for-all.patch"
        "0013-Add-Z3-to-system-libraries-list-if-enabled.patch"
        "0101-Allow-build-static-clang-library-for-mingw.patch"
        "0102-fix-libclang-name-for-mingw.patch"
        "0104-link-pthread-with-mingw.patch"
        "0105-build-libclang-cpp-fix.patch"
        "0106-cmake-Fix-build-of-attribute-plugin-example-on-Windo.patch"
        "0301-LLD-COFF-Error-out-if-creating-a-DLL-with-too-many-e.patch"
        "0302-LLD-Allow-configuring-default-ld.lld-backend.patch"
        "0303-LLD-Ignore-ELF-tests-when-ld.lld-defaults-to-MinGW.patch"
        "0304-ignore-new-bfd-options.patch"
        "0305-use-TerminateProcess-instead-of-exit.patch"
        "0504-fix-lldb-library-name-mingw.patch"
        "0801-Don-t-build-LLVMPolly-on-WIN32.patch")
# Some patch notes :)
#0001-0099 -> llvm
#0101-0199 -> clang
#0201-0299 -> rt
#0301-0399 -> lld
#0401-0499 -> libc++
#0501-0599 -> lldb
#0601-0699 -> libunwind
#0701-0799 -> libc++abi
#0801-0899 -> polly
#0901-0999 -> openmp
sha256sums=('913f68c898dfb4a03b397c5e11c6a2f39d0f22ed7665c9cefa87a34423a72469'
            'SKIP'
            '2d704df8ca67b77d6d94ebf79621b0f773d5648963dd19e0f78efef4404b684c'
            'SKIP'
            '0f96acace1e8326b39f220ba19e055ba99b0ab21c2475042dbc6a482649c5209'
            'SKIP'
            '374aff82ff573a449f9aabbd330a5d0a441181c535a3599996127378112db234'
            'SKIP'
            'add4fc302628b89f238ae95cb594061b57b6482ddf6e92f930718d87fda58619'
            'SKIP'
            '6c1ee6690122f2711a77bc19241834a9219dda5036e1597bfa397f341a9b8b7a'
            'SKIP'
            '58697d4427b7a854ec7529337477eb4fba16407222390ad81a40d125673e4c15'
            'SKIP'
            'fed318f75d560d0e0ae728e2fb8abce71e9d0c60dd120c9baac118522ce76c09'
            'SKIP'
            'efe7be4a7b7cdc6f3bcf222827c6f837439e6e656d12d6c885d5c8a80ff4fd1c'
            'SKIP'
            '8570c09f57399e21e0eea0dcd66ae0231d47eafc7a04d6fe5c4951b13c4d2c72'
            'SKIP'
            '8455011c33b14abfe57b2fd9803fb610316b16d4c9818bec552287e2ba68922f'
            'SKIP'
            'dcfadb8d11f2ea0743a3f19bab3b43ee1cb855e136bc81c76e2353cd76148440'
            'SKIP'
            '9b6d3ecb0ef4a38d34aefaefff8c6257ff22d366d84630020d7f079dc8065d97'
            '1f318c0370357fdf9c54ae6d31bad761b0caa58ac099998937b636309ecb6590'
            '7f0c64cd87b61e894be632f180ae5291e1aa9f1d9d382608f659067eeeda7146'
            '2c2431997e664c5b42b359f4134a4773578753e5e25c505ba30de42d357f3057'
            '1887ea21fcd591a50dd10559774e872ce1183177e672b028133d9b669e3cac32'
            'e1c5f6fa15d6ce9af24dde2b038f0c2fb26a18f2f1200c65405adca6f8d15a6a'
            '33400d16d5f6671a8fd60345c3ae44b9777a7d600061957889d14305eb2ad709'
            '1c9efccd40a0e7834c3aa9d819aa25cfdd2cec389d1bd3e8a89bc9ff670a0129'
            'a2f0e8090a9450abe51e43ba9e9c8ff4b2a4024ba394deea4b8aff547e30fc33'
            'ddc43bc16f5edaf5290adabd71ce6a8a593fc3f824d7d99acd247bcce88aaf03'
            '01b029f2a21bd998ce374a90d41d214c891dfbb611dfbd9ca147517cd2c228ea'
            '53646dd01af2862473e9719c5223366486268891ccbff86413943a432a8342e9'
            'a60f7328d84628a56a9f626e4dc26ffd0c35292c79eeba62ac3d4f25aef2fe5c'
            '0098da33ce4cfea2a6b6943c15e769345f89b84ebea28facff4cc8b92a17bc8f'
            '0b996f438f7c7bf42c789729dfd588a65d5016386e7032e03e8cd52f1dc8bc73'
            'c74c313e442a5b8fed7c6372ac8ff8f3598c9e33db1b520f1297949e18042e55'
            '2e1705274dfc55466cc8977e61d569a685e18ce07895cbec2ccf3b848eafd8ee'
            '778e0db0a5b0657ab05e2a81d83241347a4a41af2b0f9903422f651fa58e8d40'
            '9482cde0ae847c4435ec2294805bdaecac3f6b9eea30c89bd7fca15103cf2728'
            'bed7ae642486a8fd0db5afb0b9afcd8a87605de318ad891bf4c9378a2eb09dc2'
            'bc394e597f8939b6f6630bd88c990f951738aaadacded2f3be71c658e9608fe7')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A') # Tom Stellard
noextract=(clang-${pkgver}.src.tar.xz
           libcxx-${pkgver}.src.tar.xz
           lldb-${pkgver}.src.tar.xz
           test-suite-${pkgver}.src.tar.xz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  plain "Extracting clang-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/clang-${pkgver} ]] && rm -rf ${srcdir}/clang-${pkgver}
  [[ -d ${srcdir}/clang-${pkgver} ]] || tar -xJvf ${srcdir}/clang-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting libcxx-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/libcxx-${pkgver} ]] && rm -rf ${srcdir}/libcxx-${pkgver}
  [[ -d ${srcdir}/libcxx-${pkgver} ]] || tar -xJvf ${srcdir}/libcxx-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting lldb-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/lldb-${pkgver} ]] && rm -rf ${srcdir}/lldb-${pkgver}
  [[ -d ${srcdir}/lldb-${pkgver} ]] || tar -xJvf ${srcdir}/lldb-${pkgver}.src.tar.xz -C ${srcdir} || true

  cd "${srcdir}/llvm-${pkgver}.src"
  apply_patch_with_msg \
      "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch" \
      "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch" \
      "0005-add-pthread-as-system-lib-for-mingw.patch" \
      "0008-enable-emutls-for-mingw.patch" \
      "0009-empty-target-prefix-only-msvc.patch" \
      "0010-mbig-obj-for-all.patch" \
      "0013-Add-Z3-to-system-libraries-list-if-enabled.patch"

  # https://bugs.llvm.org/show_bug.cgi?id=25493
  apply_patch_with_msg \
      "0006-add-coff-exported-flag.patch"
  # https://bugs.llvm.org/show_bug.cgi?id=30584
  apply_patch_with_msg \
      "0007-getLoadAddressWithOffset-assertion.patch"

  cd "${srcdir}/clang-${pkgver}.src"
  apply_patch_with_msg \
      "0101-Allow-build-static-clang-library-for-mingw.patch" \
      "0102-fix-libclang-name-for-mingw.patch" \
      "0104-link-pthread-with-mingw.patch" \
      "0105-build-libclang-cpp-fix.patch" \
      "0106-cmake-Fix-build-of-attribute-plugin-example-on-Windo.patch"

  cd "${srcdir}/lld-${pkgver}.src"
  apply_patch_with_msg \
      "0301-LLD-COFF-Error-out-if-creating-a-DLL-with-too-many-e.patch" \
      "0302-LLD-Allow-configuring-default-ld.lld-backend.patch" \
      "0303-LLD-Ignore-ELF-tests-when-ld.lld-defaults-to-MinGW.patch" \
      "0304-ignore-new-bfd-options.patch" \
      "0305-use-TerminateProcess-instead-of-exit.patch"

  cd "${srcdir}/lldb-${pkgver}.src"
  apply_patch_with_msg \
      "0504-fix-lldb-library-name-mingw.patch"

  cd "${srcdir}/polly-${pkgver}.src"
  # With LLVM_LINK_LLVM_DYLIB=ON it's build before LLVM and fails with undefined references to LLVM functions
  apply_patch_with_msg \
      "0801-Don-t-build-LLVMPolly-on-WIN32.patch"

  cd "${srcdir}"
  rm -rf clang clang-tools-extra lld lldb polly compiler-rt libcxx \
         libcxxabi libunwind openmp test-suite | true
  mv "${srcdir}/clang-${pkgver}.src"             clang
  mv "${srcdir}/clang-tools-extra-${pkgver}.src" clang-tools-extra
  mv "${srcdir}/lld-${pkgver}.src"               lld
  mv "${srcdir}/lldb-${pkgver}.src"              lldb
  mv "${srcdir}/polly-${pkgver}.src"             polly
  mv "${srcdir}/compiler-rt-${pkgver}.src"       compiler-rt
  mv "${srcdir}/libcxxabi-${pkgver}.src"         libcxxabi
  mv "${srcdir}/libcxx-${pkgver}.src"            libcxx
  mv "${srcdir}/libunwind-${pkgver}.src"         libunwind
  mv "${srcdir}/openmp-${pkgver}.src"            openmp
  #mv "${srcdir}/testsuite-${pkgver}.src"         test-suite
}

build() {
  cd "${srcdir}"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
    VERBOSE="VERBOSE=1"
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  if [ "${_compiler}" == "clang" ]; then
    export CC='clang'
    export CXX='clang++'
    extra_config+=(-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON -DLLVM_ENABLE_LIBCXX=ON)
  fi

  export ASM_COMPILER="${MINGW_PREFIX}/bin/uasm.exe"
  if [ "${CARCH}" == "x86_64" ]; then
    export ASM_COMPILER_FLAGS=-win64
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_ASM_MASM_COMPILER="${ASM_COMPILER}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DLIBCLANG_BUILD_STATIC=ON \
    -DLIBOMP_ASMFLAGS="${ASM_COMPILER_FLAGS}" \
    -DLIBOMP_FORTRAN_MODULES=ON \
    -DLLD_DEFAULT_LD_LLD_IS_MINGW=ON \
    -DLLDB_ENABLE_LIBEDIT=OFF \
    -DLLDB_USE_SYSTEM_SIX=ON \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DLLVM_BUILD_STATIC=OFF \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt;lld;lldb;openmp;polly" \
    -DLLVM_ENABLE_SPHINX=ON \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DLLVM_POLLY_LINK_INTO_TOOLS=OFF \
    -DPython3_FIND_REGISTRY=NEVER \
    -DPython3_ROOT_DIR=${MINGW_PREFIX} \
    -DLLDB_EMBED_PYTHON_HOME=OFF \
    "${extra_config[@]}" \
    ../llvm-${pkgver}.src

  # sed away the bad windows style command line parameters (they are translated by msys from /bad to c:\msys\bad
  # Ninja doesn't need fixing them.
  sed -i.orig 's/\/c \/Fo/-c -Fo/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make
  sed -i.orig 's/\/safeseh \/coff/-safeseh -coff/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make

  ${_make_cmd}

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  #
  sed -i.orig '/\(clang\|lld\|lldb\|polly\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i.orig '/\(extra\|scan-build\|scan-view\)\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
  sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\|openmp\|libunwind\)\/cmake_install.cmake/d' projects/cmake_install.cmake

  # Use newly built compiler because GCC/binutils doesn't play nicely
  export CC="${srcdir}/build-${CARCH}/bin/clang"
  export CXX="${srcdir}/build-${CARCH}/bin/clang++"
  # Force win32 threads for libc++abi
  export CXXFLAGS="${CXXFLAGS} -D_LIBCPP_HAS_THREAD_API_WIN32"
  COMMON_CMAKE_ARGS=(-DCMAKE_AR=${srcdir}/build-${CARCH}/bin/llvm-ar
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}
    -DCMAKE_RANLIB=${srcdir}/build-${CARCH}/bin/llvm-ranlib
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib
    -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=ON
    -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
    -DLIBCXX_HAS_WIN32_THREAD_API=ON
    -DLIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS=OFF
    -DLIBCXXABI_ENABLE_SHARED=OFF
    -DLIBCXXABI_ENABLE_STATIC=ON
    -DLIBUNWIND_ENABLE_SHARED=ON
    -DLIBUNWIND_USE_COMPILER_RT=ON
    -DLLVM_ENABLE_LLD=ON
    -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi;libunwind"
    -DPython3_FIND_REGISTRY=NEVER
    -DPython3_ROOT_DIR=${MINGW_PREFIX})

  cd "${srcdir}"

  [[ -d build-libcxx-shared-${CARCH} ]] && rm -rf build-libcxx-shared-${CARCH}
  mkdir build-libcxx-shared-${CARCH} && cd build-libcxx-shared-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_LIBCPP_BUILDING_LIBRARY -U_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS" \
    -DLIBCXX_ENABLE_SHARED=ON \
    -DLIBCXX_ENABLE_STATIC=OFF \
    -DLIBUNWIND_ENABLE_STATIC=OFF \
    "${COMMON_CMAKE_ARGS[@]}" \
    ${extra_config[@]} \
    ../llvm-${pkgver}.src

  ${_make_cmd} cxx

  cd "${srcdir}"

  [[ -d build-libcxx-static-${CARCH} ]] && rm -rf build-libcxx-static-${CARCH}
  mkdir build-libcxx-static-${CARCH} && cd build-libcxx-static-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS" \
    -DLIBCXX_ENABLE_SHARED=OFF \
    -DLIBCXX_ENABLE_STATIC=ON \
    -DLIBUNWIND_ENABLE_STATIC=ON \
    "${COMMON_CMAKE_ARGS[@]}" \
    ${extra_config[@]} \
    ../llvm-${pkgver}.src

  ${_make_cmd} unwind cxxabi cxx
}

check() {
 cd "${srcdir}"/build-${CARCH}
 # Remove || true once testcase doesn't cause failures.
#  make check || true
#  make check-clang || true
#  make check-polly || true
  make check-lld || true
}

package_clang() {
  pkgdesc="C language family frontend for LLVM (mingw-w64)"
  url="https://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-z3")

  cd "${srcdir}/clang"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang DESTDIR="${pkgdir}" install

  # Install static libraries
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang.a
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang_static.a
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework (mingw-w64)"
  url="https://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}/clang"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-build DESTDIR="${pkgdir}" install
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-view DESTDIR="${pkgdir}" install

  # Use Python 2
  sed -i \
    -e 's|env python$|&2|' \
    -e 's|/usr/bin/python$|&2|' \
    "${pkgdir}"${MINGW_PREFIX}/bin/scan-view

  # Compile Python scripts
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}"${MINGW_PREFIX}/bin/clang-analyzer
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}"${MINGW_PREFIX}/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra() {
  pkgdesc="Extra tools built using Clang's tooling APIs (mingw-w64)"
  url="https://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}-${pkgrel}")

  cd "${srcdir}/clang-tools-extra"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/extra DESTDIR="${pkgdir}" install
}

package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM (mingw-w64)"
  url="https://compiler-rt.llvm.org/"

  cd "${srcdir}"/compiler-rt
  ${_make_cmd} -C ../build-${CARCH}/projects/compiler-rt DESTDIR="${pkgdir}" install
}

package_libcxx() {
  pkgdesc="C++ Standard Library (mingw-w64)"
  url="https://libcxx.llvm.org/"
  depends="${MINGW_PACKAGE_PREFIX}-gcc-libs"

  cd "${srcdir}/libcxx"
  ${_make_cmd} -C ../build-libcxx-shared-${CARCH}/projects/libcxx DESTDIR="${pkgdir}" install
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libcxx DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/build-libcxx-static-${CARCH}/lib/libc++experimental.a" "${pkgdir}${MINGW_PREFIX}/lib/libc++experimental.a"
}

package_openmp() {
  pkgdesc="OpenMP library (mingw-w64)"
  url="https://openmp.llvm.org/"

  cd "${srcdir}/openmp"
  ${_make_cmd} -C ../build-${CARCH}/projects/openmp DESTDIR="${pkgdir}" install
}

package_libcxxabi() {
  pkgdesc="C++ Standard Library Support (mingw-w64)"
  url="https://libcxxabi.llvm.org/"
  depends="${MINGW_PACKAGE_PREFIX}-gcc-libs"

  cd "${srcdir}/libcxxabi"
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libcxxabi DESTDIR="${pkgdir}" install
}

package_libunwind() {
  pkgdesc='A new implementation of a stack unwinder for C++ exceptions (mingw-w64)'
  url='https://llvm.org/'

  cd "${srcdir}/libunwind"
  ${_make_cmd} -C ../build-libcxx-static-${CARCH}/projects/libunwind DESTDIR="${pkgdir}" install

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  install "${srcdir}/build-libcxx-static-${CARCH}/lib/libunwind.dll" "${pkgdir}${MINGW_PREFIX}/bin/libunwind.dll"
}

package_lld() {
  pkgdesc="Linker tools for LLVM (mingw-w64)"
  url="https://lld.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}/lld"
  ${_make_cmd} -C ../build-${CARCH}/tools/lld DESTDIR="${pkgdir}" install
}

package_lldb() {
  pkgdesc="Next generation, high-performance debugger (mingw-w64)"
  url="https://lldb.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-lua"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-readline"
           "${MINGW_PACKAGE_PREFIX}-swig")

  cd "${srcdir}/lldb"

  ${_make_cmd} -C ../build-${CARCH}/tools/lldb DESTDIR="${pkgdir}" install

  # Compile Python scripts
   local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/lldb/LICENSE"
}

package_llvm() {
  pkgdesc="Low Level Virtual Machine (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-z3")

  cd "${srcdir}"/llvm-${pkgver}.src

  ${_make_cmd} -C ../build-${CARCH} DESTDIR="${pkgdir}" install

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/llvm/LICENSE"

  # Install CMake stuff
  install -d "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/modules/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/platforms/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/platforms/

  # FileCheck is needed to build rust.
  install -Dm755 "${srcdir}"/build-${CARCH}/bin/FileCheck.exe "${pkgdir}${MINGW_PREFIX}/bin/FileCheck.exe"
  # fix cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}/${MINGW_PREFIX}/lib/cmake/llvm/LLVMExports.cmake
}

package_polly() {
  pkgdesc="Polly - Polyhedral optimizations for LLVM (mingw-w64)"
  url="https://polly.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/polly
  ${_make_cmd} -C ../build-${CARCH}/tools/polly DESTDIR="${pkgdir}" install
  # fix cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  local SRC_WIN=$(cygpath -wm ${srcdir})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" \
    -e "s|${SRC_WIN}/build-${CARCH}/lib|${MINGW_PREFIX}/lib|g" \
    -i "${pkgdir}/${MINGW_PREFIX}/lib/cmake/polly/PollyConfig.cmake"
}

# Wrappers
package_mingw-w64-i686-clang(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-i686-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-i686-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-i686-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-i686-libc++(){
  package_libcxx
}

package_mingw-w64-i686-openmp(){
  package_openmp
}

package_mingw-w64-i686-lld(){
  package_lld
}

package_mingw-w64-i686-lldb(){
  package_lldb
}

package_mingw-w64-i686-libunwind(){
  package_libunwind
}

package_mingw-w64-i686-llvm(){
  package_llvm
}

package_mingw-w64-i686-polly(){
  package_polly
}

package_mingw-w64-x86_64-clang(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-x86_64-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-x86_64-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-x86_64-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-x86_64-libc++(){
  package_libcxx
}

package_mingw-w64-x86_64-openmp(){
  package_openmp
}

package_mingw-w64-x86_64-lld(){
  package_lld
}

package_mingw-w64-x86_64-lldb(){
  package_lldb
}

package_mingw-w64-x86_64-libunwind(){
  package_libunwind
}

package_mingw-w64-x86_64-llvm(){
  package_llvm
}

package_mingw-w64-x86_64-polly(){
  package_polly
}
